﻿
package extremefx.conditions {

	/**
	 * @author Marcelo Volmaro
	 */
	public final class NumberValidator extends Validator {

		private var _number:Number;
		
		public function NumberValidator(pValue:Number, pArgumentName:String, pAdditionalMessage:String = null) {
			super(pValue, pArgumentName, pAdditionalMessage);
			_number = pValue;
		}				public function isNumber(pDescription:String = null):NumberValidator {
			if (!isNaN(_value)) return this;
			
			throw _buildException(
				_getFormattedConditionMessage(
					"{0} should be a number", 
					pDescription, 
					_argumentName
				)
			);
		}
		public function isInRange(pMin:Number, pMax:Number, pDescription:String = null):NumberValidator {
			if (pMin <= _number && _number <= pMax) return this;
			
			throw _buildException(
				_getFormattedConditionMessage(
					Resources.m_betAB, 
					pDescription, 
					_argumentName,
					pMin,
					pMax
				),
				
				RangeError
			);
		}
		
		public function isNotInRange(pMin:Number, pMax:Number, pDescription:String = null):NumberValidator {
			if (_number <= pMin || pMax <= _number) return this;
			
			throw _buildException(
				_getFormattedConditionMessage(
					Resources.m_notBetAB, 
					pDescription, 
					_argumentName,
					pMin,
					pMax
				),
				
				RangeError
			);
		}
		
		public function isGreaterThan(pMin:Number, pDescription:String = null):NumberValidator {
			if (pMin < _number) return this;
			
			throw _buildException(
				_getFormattedConditionMessage(
					Resources.m_greatA, 
					pDescription, 
					_argumentName,
					pMin
				),
				
				RangeError
			);
		}
		
		public function isGreaterOrEqualThan(pMin:Number, pDescription:String = null):NumberValidator {
			if (pMin <= _number) return this;
			
			throw _buildException(
				_getFormattedConditionMessage(
					Resources.m_greatEqA, 
					pDescription, 
					_argumentName,
					pMin
				),
				
				RangeError
			);
		}
		
		public function isLessThan(pMax:Number, pDescription:String = null):NumberValidator {
			if (_number < pMax) return this;
			
			throw _buildException(
				_getFormattedConditionMessage(
					Resources.m_smallA, 
					pDescription, 
					_argumentName,
					pMax
				),
				
				RangeError
			);
		}
		
		public function isLessOrEqualThan(pMax:Number, pDescription:String = null):NumberValidator {
			if (_number <= pMax) return this;
			
			throw _buildException(
				_getFormattedConditionMessage(
					Resources.m_smallEqA, 
					pDescription, 
					_argumentName,
					pMax
				),
				
				RangeError
			);
		}
		
		public function isEqualTo(pValue:Number, pDescription:String = null):NumberValidator {
			if (_number == pValue) return this;
			
			throw _buildException(
				_getFormattedConditionMessage(
					Resources.m_eqA, 
					pDescription, 
					_argumentName,
					pValue
				)
			);
		}
		
		public function isNotEqualTo(pValue:Number, pDescription:String = null):NumberValidator {
			if (_number != pValue) return this;
			
			throw _buildException(
				_getFormattedConditionMessage(
					Resources.m_notEqA, 
					pDescription, 
					_argumentName,
					pValue
				)
			);
		}
	}
}
